using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Oracle.DataAccess.Client;
using PluginHandler.Database;

namespace OraclePlugin.Database
{
    public class DatabaseView : IDatabaseView
    {
        private DatabaseHandler _databaseHandler;
        private string _name;
        private List<IDatabaseTableColumn> _columns;
        private string _queryString;

        internal DatabaseView(DatabaseHandler databaseHandler, string name)
        {
            _databaseHandler = databaseHandler;

            _name = name;
            _columns = null;
            _queryString = null;
        }

        #region IDatabaseView Member

        public List<IDatabaseTableColumn> Columns
        {
            get 
            {
                if (_columns == null)
                {
                    _columns = new List<IDatabaseTableColumn>();

                    DataTable dataTable = GetTableSchema();

                    foreach (DataRow dataRow in dataTable.Rows)
                    {
                        string columnName = dataRow["COLUMN_NAME"].ToString();
                        string schemaType = dataRow["DATATYPE"].ToString();
                        DbType dataType = DatabaseTableColumn.ConvertStringToDbType(schemaType);

                        _columns.Add(
                            new DatabaseTableColumn(_databaseHandler, this, columnName, dataType, schemaType));
                    }
                }

                return _columns; 
            }
        }

        public string QueryString
        {
            get 
            {
                if (_queryString == null)
                {
                    _queryString = GetViewQueryString();
                }
                
                return _queryString; 
            }
        }

        #endregion

        #region IDatabaseObject Member

        public string Name
        {
            get { return _name; }
        }

        public string DelimitedIdentifierName
        {
            get
            {
                return _databaseHandler.DelimitedIdentifierBegin + _name + _databaseHandler.DelimitedIdentifierEnd;
            }
        }

        public DataTable GetTableSchema()
        {
            OracleConnection sqlConnection = (OracleConnection)_databaseHandler.DatabaseConnection;
            DataTable schemaInformation = sqlConnection.GetSchema(
                "Columns",
                new string[] 
                { 
                    (string)_databaseHandler.ConnectionStringDetails["User Id"],
                    _name
                });

            return schemaInformation;
        }

        public string GetViewQueryString()
        {
            string queryString = string.Empty;

            OracleConnection sqlConnection = (OracleConnection)_databaseHandler.DatabaseConnection;
            DataTable schemaInformation = sqlConnection.GetSchema(
                "Views",
                new string[] 
                { 
                    (string)_databaseHandler.ConnectionStringDetails["User Id"],
                    _name
                });

            foreach (DataRow dataRow in schemaInformation.Rows)
            {
                queryString = dataRow["TEXT"].ToString();
            }

            return queryString;
        }

        #endregion
    }
}
